#include "ctrl_core.h"
#include "ctrl_init.h"
#include "ctrl_offline_manager.h"
#include "App.h"








uint16_t time_cnt = 0;
void Ctrl_Hron_H(void)
{
	GPIO_Out(&CtrlPm.HRON,OPEN);
}


void Ctrl_Hron_L(void)
{
	GPIO_Out(&CtrlPm.HRON,CLOSE);
}

uint8_t TimeCnt(uint16_t sum_cnt)
{
	if(time_cnt >= sum_cnt)
	{
		time_cnt = 0;
		return 1;
	}
	else
	{
		time_cnt++;
		return 0;
	}
}








uint8_t close_pro = 0;
uint8_t open_pro = 0;
void RemoteCloseCtrl(void)
{
	switch(close_pro)
	{
		case 0:
	    	SetFinishFlag(0);
		    Send_Server_pro(0xC0);
			master_send_to_server_answer();
		    close_pro++;
		    break;
		case 1:
			GPIO_Out(&CtrlPm.RC_SW,OPEN);
		  if(TimeCnt(12))
		      close_pro++;
			break;
		case 2:
			GPIO_Out(&CtrlPm.RC_SW,CLOSE);
		  if(TimeCnt(12))
		      close_pro++;
			break;
	  case 3:
			GPIO_Out(&CtrlPm.RC_SW,OPEN);
		  if(TimeCnt(72))
		      close_pro++;
			break;
		case 4:
			GPIO_Out(&CtrlPm.RC_SW,CLOSE);
		  if(TimeCnt(9))
		      close_pro++;
			break;
	  case 5:
		   Send_Server_pro(0xC0);
		  SetFinishFlag(1);
	      master_send_to_server_answer();
		  close_pro++;
		  break;
	  
	  default:
			Clear_Pro();
	  break;
	}	
}



uint8_t FRCC_Pro = 0;
void RCFroceCloseCtrl(void)
{
	switch(FRCC_Pro)
	{
		case 0:
		{
	    	SetFinishFlag(0);
		    Send_Server_pro(0xC2);
			master_send_to_server_answer();
		    FRCC_Pro++;
		    break;
		}
	    case 1:
	    {
		    GPIO_Out(&CtrlPm.RC_SW,OPEN);
		    if(TimeCnt(180))
 		        FRCC_Pro++;
			break;
   	    }
		case 2:
		{
			GPIO_Out(&CtrlPm.RC_SW,CLOSE);
		    if(TimeCnt(30))
 		        FRCC_Pro++;
		}
		case 3:
		{
		  SetFinishFlag(1);
		  Send_Server_pro(0xC2);
	      master_send_to_server_answer();
		  FRCC_Pro++;
		  break;
		}
		default:
		{
			Clear_Pro();
	        break;
		}
  }
}



void RemoteOpenCtrl(void)
{
	switch(open_pro)
	{
		case 0:
		{
			close_pro = 0;
		    SetFinishFlag(0);
			Send_Server_pro(0xC1);
			master_send_to_server_answer();
		    open_pro++;
		}
		case 1:
		{
		    GPIO_Out(&CtrlPm.RC_SW,OPEN);
		    if(TimeCnt(12))
		        open_pro++;
		 	break;
		}
		case 2:
		{
			GPIO_Out(&CtrlPm.RC_SW,CLOSE);
		    if(TimeCnt(12))
		        open_pro++;
			break;
		}
		case 3:
		{
			GPIO_Out(&CtrlPm.RC_SW,OPEN);
		    if(TimeCnt(72))
		        open_pro++;
			break;
		}
		case 4:
		{
			GPIO_Out(&CtrlPm.RC_SW,CLOSE);
		    if(TimeCnt(12))
		      open_pro++;
			break;
		}
		case 5:
		{
			SetFinishFlag(1);
			Ax_State[remote_state] = 0x01;
			Send_Server_pro(0xC1);
	    	master_send_to_server_answer();
		    open_pro++;
		}
		default:
		{
			Clear_Pro();
		    break;
		}
	}
}







